package com.oneguy.client.log;

import org.apache.log4j.*;

public class LoggerFactory {
	private static final String MY_LOG_LAYOUT = "%d{yyyy/MM/dd HH:mm:ss:SSS} %5p [%t] - %m%n";
	private static final Level P2P_LOG_LEVEL = Level.ALL;
	public static Logger getIndependentLogger(String name) {
		Logger logger = Logger.getLogger(name);
		logger.removeAllAppenders();
		logger.setLevel(P2P_LOG_LEVEL);
		logger.setAdditivity(true);
		logger.addAppender(getNamedAppender(name));
		return logger;
	}
	
	public static Logger getDefaultLogger(){
		return getRootLogger("default");
	}
	public static Logger getRootLogger(String name){
		Logger logger = Logger.getLogger(name);
		logger.setLevel(P2P_LOG_LEVEL);
		return logger;
	}
	
	private static Appender getNamedAppender(String name) {
		FileAppender appender = new BsiRollingFileAppender();
		appender.setFile("./log/server/yyyy-MM-dd/" + name + ".log");
		appender.setLayout(getMyLayour());
		appender.setEncoding("UTF-8");
		appender.setAppend(true);
		appender.activateOptions();
		return appender;
	}

	private static Layout getMyLayour() {
		PatternLayout layout = new PatternLayout();
        layout.setConversionPattern(MY_LOG_LAYOUT);
		return layout;
	}

	public static void main(String[] args) {
		String TAG1 = "tag1";
		String TAG2 = "tag2";
		Log.d(TAG1, "d1");
		Log.d(TAG2, "d2");
		Log.e(TAG1, "e1");
		Log.e(TAG2, "e2");
		Exception e = new Exception("test exception");
		Log.e(TAG2, "test eee",e);
	}

}
